#include <iostream>
#include <string>
#include <gtest/gtest.h>
#include "base/logging.h"
#include "base/curl_client.h"
#include "base/hash.h"
#include "rapidjson/document.h"
#include "rapidjson/prettywriter.h"
#include "rapidjson/stringbuffer.h"
#include <sstream>

using namespace std;
using namespace rapidjson;

const int repeat_times = 1000;
const int kThreadCount = 5;

void * ThreadFun(void * data) {
  int id = *(int*)data;

  for (int i=0; true; ++i) {
//for (int i=0; i<repeat_times; ++i){
    try {
//    {   
//      string response;
//      stringstream post_body;
//      post_body << "ans_id=1385368501104138";
//      post_body << "&app=" << "53629aab8b031ee5cb1c819b4329c134";
//      post_body << "&ask_id=1385024248103941";
//      post_body << "&method=qa.ask.getAppendByAnswer";
//      post_body << "&t=1387248368";
//      post_body << "&v=1.0";

//      string md5_original_str = post_body.str();
//      md5_original_str += "&";
//      md5_original_str += "00bfc08b6a9ba1eaaa5b4e01d30239a4";
//      string md5_str = base::HashMD5(md5_original_str.c_str(), md5_original_str.length());
//      post_body << "&sign=" << md5_str;
//
//      string url("cp.api.wenda.so.com/intf/i10");
//      url += "?";
//      url += post_body.str();
//      if (base::CurlClient::Instance().Get( url, &response)<0)
//      {
//        LOG_DEBUG(id <<" Get Failed");
//      }
//      LOG_DEBUG(id << " " << md5_original_str << " " << md5_str << " " << url << " " << response);
//    }

      // SetRead
      {
//      string response;
//      string set_read_url("http://10.16.15.138/phone/set_read?userId=281122896&questionId=1385979449104911&responserId=508186080&model=gt-i9300&key=1589791beaeca44ac84df427d26209a9");
//      LOG_INFO("SetRead start : " << set_read_url);

//      if (base::CurlClient::Instance().Get( set_read_url, &response)<0) {
//        LOG_WARN("SetRead failed : " << set_read_url);
//        return NULL;
//      }

//      Document document;
//      document.Parse<0>(response.c_str());
//      if (document.HasParseError()) {
//        LOG_WARN("SetRead failed : " << response);
//        return NULL;
//      }

//      StringBuffer str_buff;
//      PrettyWriter<StringBuffer> writer(str_buff);
//      document.Accept(writer);
//      string output(str_buff.GetString(), str_buff.Size());
//      LOG_INFO("SetRead finish : " << output);
      }

      // GetMessageList
      {
//      string response;
//      string get_message_list_url("http://10.16.15.138/phone/get_message_list?userId=281122896&questionId=1385024248103941&responserId=242736791&answerId=1385368501104138&model=GT-9100&key=123");
//      LOG_INFO(id << " GetMessageList start : " << get_message_list_url);
//       
//      if (base::CurlClient::Instance().Get( get_message_list_url, &response)<0) {
//        LOG_WARN(id << " GetMessageList failed : " << get_message_list_url);
//        return NULL;
//      }
//      
//      Document document;
//      document.Parse<0>(response.c_str());
//      if (document.HasParseError()) {
//        LOG_WARN(id << " GetMessageList failed : " << response);
//        return NULL;
//      }
//      
//      StringBuffer str_buff;
//      PrettyWriter<StringBuffer> writer(str_buff);
//      document.Accept(writer);
//      string output(str_buff.GetString(), str_buff.Size());
//      LOG_INFO(id << " GetMessageList finish : " << output);
      }

      // GetQuestionList
      {
        string response;
        string get_question_list_url("http://10.16.15.138/phone/get_question_list?model=GT-I9300&userId=141777275&start=0&count=100&key=994935efa341976b81ef36377d53fd89");
        LOG_INFO("GetQuestionList start : " << get_question_list_url);

        if (base::CurlClient::Instance().Get( get_question_list_url, &response)<0) {
          LOG_WARN("GetQuestionList failed : " << get_question_list_url);
          return NULL;
        }
        
        Document document;
        document.Parse<0>(response.c_str());
        if (document.HasParseError()) {
          LOG_WARN("GetQuestionList failed : " << response);
          return NULL;
        }

        StringBuffer str_buff;
        PrettyWriter<StringBuffer> writer(str_buff);
        document.Accept(writer);
        string output(str_buff.GetString(), str_buff.Size());
        LOG_INFO("GetQuestionList finish : " << output);
      }

      // GetQuestionSessionList
      {
//      string response;
//      string get_question_session_list_url("http://10.16.15.138/phone/get_question_session_list?model=sdk&userId=281122895&questionId=1385024248103941&key=4529874064c8ff7cd64d6a7b23a8e857");
//      LOG_INFO("GetQuestionSessionList start : " << get_question_session_list_url);

//      if (base::CurlClient::Instance().Get( get_question_session_list_url, &response)<0) {
//        LOG_WARN("GetQuestionSessionList failed : " << get_question_session_list_url);
//        return NULL;
//      }

//      Document document;
//      document.Parse<0>(response.c_str());
//      if (document.HasParseError()) {
//        LOG_WARN("GetQuestionSessionList failed : " << response);
//        return NULL;
//      }

//      StringBuffer str_buff;
//      PrettyWriter<StringBuffer> writer(str_buff);
//      document.Accept(writer);
//      string output(str_buff.GetString(), str_buff.Size());
//      LOG_INFO("GetQuestionSessionList finish : " << output);
      }

      // GetSessionList
      {
//      string response;
//      string get_session_list_url("http://10.16.15.138/phone/get_session_list?userId=281122895&model=huawei+u9508&last_time=0&key=123");
//      LOG_INFO("GetSessionList start : " << get_session_list_url);

//      if (base::CurlClient::Instance().Get( get_session_list_url, &response)<0) {
//        LOG_WARN("GetSessionList failed : " << get_session_list_url);
//        return NULL;
//      }

//      Document document;
//      document.Parse<0>(response.c_str());
//      if (document.HasParseError()) {
//        LOG_WARN("GetSessionList failed : " << response);
//        return NULL;
//      }

//      StringBuffer str_buff;
//      PrettyWriter<StringBuffer> writer(str_buff);
//      document.Accept(writer);
//      string output(str_buff.GetString(), str_buff.Size());
//      LOG_INFO("GetSessionList finish : " << output);
      }
      
    } catch (const std::exception & e) {
      std::cout << " error:" << e.what() << std::endl;
    }
    
  }

  return NULL;
}

int main() {
  LOG_INIT("./log/pe_ask_proxy_service_test.log", "DEBUG");

  pthread_t ts[kThreadCount];
  int arr[3];
  for (int i = 0; i < kThreadCount; ++i) {
    arr[i] = i;
    pthread_create(&ts[i], NULL, ThreadFun, &arr[i]);
  }

  for (int i = 0; i < kThreadCount; ++i) {
    pthread_join(ts[i], NULL);
  }

  return 0;
}
